package com.hui.servlet;


import com.hui.entity.Manager;
import com.hui.entity.Role;
import com.hui.service.ManagerService;
import com.hui.service.RoleService;
import com.hui.vo.ManagerVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description 管理员控制层
 */
@WebServlet(urlPatterns = {"/findByMgrId", "/updateManager","/findMyselfMgrId", "/findManagerAll", "/delManagerById", "/toAddManagerPage", "/addManager", "/toUpdateManager", "/toUpdateMyself", "/updateMyself"})
public class ManagerServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        String uri = req.getRequestURI();
        if (uri.endsWith("findByMgrId")) {
            findByMgrId(req, resp);
        } else if (uri.endsWith("updateManager")) {
            updateManager(req, resp);
        } else if (uri.endsWith("findManagerAll")) {
            findManagerAll(req, resp);
        } else if (uri.endsWith("delManagerById")) {
            delManagerById(req, resp);
        } else if (uri.endsWith("toAddManagerPage")) {
            toAddManagerPage(req, resp);
        } else if (uri.endsWith("addManager")) {
            try {
                addManager(req, resp);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else if (uri.endsWith("toUpdateManager")) {
            toUpdateManager(req, resp);
        } else if (uri.endsWith("toUpdateMyself")) {
            toUpdateMyself(req, resp);
        } else if (uri.endsWith("updateMyself")) {
            updateMyself(req, resp);
        }else if (uri.endsWith("findMyselfMgrId")) {
            findMyselfMgrId(req, resp);
        }
    }

    /**
     * 修改管理员信息 --修改个人信息
     * 跟下面一个道理，只是跳转的界面不一样
     **/
    private void updateMyself(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        req.setCharacterEncoding("utf-8");

        String[] roleIds = req.getParameterValues("roleId");
        int mgrId = Integer.parseInt(req.getParameter("mgrId"));

        String loginName = req.getParameter("loginName");
        String loginPwd = req.getParameter("loginPwd");
        String mgrName = req.getParameter("mgrName");
        String mgrGender = req.getParameter("mgrGender");
        String mgrTel = req.getParameter("mgrTel");
        String mgrEmail = req.getParameter("mgrEmail");
        String mgrQQ = req.getParameter("mgrQQ");
        Date date = new Date();

        Manager manager = new Manager(mgrId, loginName, loginPwd, mgrName, mgrGender, mgrTel, mgrEmail, mgrQQ, date);

        ManagerService service = new ManagerService();
        service.updateManger(manager, roleIds);
        resp.sendRedirect("home.html");

    }

    /**
     * 修改管理员信息
     **/
    private void updateManager(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        req.setCharacterEncoding("utf-8");

        String[] roleIds = req.getParameterValues("roleId");
        int mgrId = Integer.parseInt(req.getParameter("mgrId"));

        String loginName = req.getParameter("loginName");
        String loginPwd = req.getParameter("loginPwd");
        String mgrName = req.getParameter("mgrName");
        String mgrGender = req.getParameter("mgrGender");
        String mgrTel = req.getParameter("mgrTel");
        String mgrEmail = req.getParameter("mgrEmail");
        String mgrQQ = req.getParameter("mgrQQ");
        Date date = new Date();

        Manager manager = new Manager(mgrId, loginName, loginPwd, mgrName, mgrGender, mgrTel, mgrEmail, mgrQQ, date);

//        System.out.println(manager);

        ManagerService service = new ManagerService();
        service.updateManger(manager, roleIds);

        resp.sendRedirect("findManagerAll");

    }

    /**
     * 跳转到修改页面--跟下面一个道理，只是跳转的界面不一样
     **/
    private void toUpdateMyself(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int mgrId = Integer.parseInt(req.getParameter("id"));

        ManagerService service = new ManagerService();
        ManagerVo managerVo = service.findByMgrId(mgrId);
        req.setAttribute("managerVo", managerVo);

        RoleService service1 = new RoleService();
        List<Role> list = service1.getAllRole();
        req.setAttribute("list", list);

        req.getRequestDispatcher("admin_myself_update.jsp").forward(req, resp);
    }

    /**
     * 跳转到修改页面
     **/
    private void toUpdateManager(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int mgrId = Integer.parseInt(req.getParameter("id"));

        ManagerService service = new ManagerService();
        ManagerVo managerVo = service.findByMgrId(mgrId);
        req.setAttribute("managerVo", managerVo);

        RoleService service1 = new RoleService();
        List<Role> list = service1.getAllRole();
        req.setAttribute("list", list);

        req.getRequestDispatcher("admin_manager_update.jsp").forward(req, resp);
    }

    /**
     * 添加管理员信息
     **/
    private void addManager(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        req.setCharacterEncoding("utf-8");

        String[] roleIds = req.getParameterValues("roleId");
        String loginName = req.getParameter("loginName");
        String loginPwd = req.getParameter("loginPwd");
        String mgrName = req.getParameter("mgrName");
        String mgrGender = req.getParameter("mgrGender");
        String mgrTel = req.getParameter("mgrTel");
        String mgrEmail = req.getParameter("mgrEmail");
        String mgrQQ = req.getParameter("mgrQQ");

        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Date createTime = dateFormat.parse(dateFormat.format(date));

        Manager manager = new Manager(null, loginName, loginPwd, mgrName, mgrGender, mgrTel, mgrEmail, mgrQQ, createTime);

        ManagerService service = new ManagerService();
        service.addManager(manager, roleIds);

        resp.sendRedirect("findManagerAll");

    }

    /**
     * 跳转到添加管理员界面
     **/
    private void toAddManagerPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService service = new RoleService();
        List<Role> list = service.getAllRole();

        req.setAttribute("list", list);
        req.getRequestDispatcher("admin_manager_add.jsp").forward(req, resp);
    }

    /**
     * 删除管理员
     **/
    private void delManagerById(HttpServletRequest req, HttpServletResponse resp) throws IOException {


        int mgrId = Integer.parseInt(req.getParameter("id"));
        ManagerService service = new ManagerService();
        service.delManagerById(mgrId);

//        删除管理员,删除角色和管理员的关联，并不删除管理员表的数据（假删除）
//        service.deleteManagerRoleByRoleId(mgrId, roleId);

//        resp.sendRedirect("findManagerAll");
    }

    /**
     * 获取全部管理员数据
     **/
    private void findManagerAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ManagerService service = new ManagerService();
        List<ManagerVo> list = service.findManagerAll();

        req.setAttribute("list", list);
        req.getRequestDispatcher("admin_manager_list.jsp").forward(req, resp);
    }


    /**
     * 根据id获取管理员信息--跟下面一个道理，只是跳转的界面不一样
     **/
    private void findByMgrId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Manager currentUser = (Manager) session.getAttribute("currentUser");

        ManagerService service = new ManagerService();
        ManagerVo managerVo = service.findByMgrId(currentUser.getMgrId());
        req.setAttribute("managerVo", managerVo);

        req.getRequestDispatcher("admin_info.jsp").forward(req, resp);
    }

    /**
     * 根据id获取管理员信息
     **/
    private void findMyselfMgrId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Manager currentUser = (Manager) session.getAttribute("currentUser");

        ManagerService service = new ManagerService();
        ManagerVo managerVo = service.findByMgrId(currentUser.getMgrId());
        req.setAttribute("managerVo", managerVo);

        req.getRequestDispatcher("admin_myself.jsp").forward(req, resp);
    }


}
